package com.study.talk.ui.app.group.create.fragment

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.study.common.ToastUtil
import com.study.talk.ui.app.base.BaseViewModel
import com.study.talk.netty.NettyClient
import com.study.talk.netty.message.request.CreateGroupRequest
import com.study.talk.utils.extras.useful
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class CreateGroupInfoViewModel @Inject constructor(application: Application) :
    BaseViewModel(application) {


    val groupAvatarUrl = MutableLiveData("")
    val groupName = MutableLiveData("")
    val groupTag = MutableLiveData("")
    val groupIntroduce = MutableLiveData("")

    fun submit(mainUser: Int, users: List<Int>) {
        if (groupAvatarUrl.value?.useful() == false) {
            ToastUtil.showToast(app, "请上传群聊图片")
            return
        }
        if (groupName.value?.useful() == false) {
            ToastUtil.showToast(app, "请输入群聊名称")
            return
        }
        if (groupTag.value?.useful() == false) {
            ToastUtil.showToast(app, "请输入群聊标签")
            return
        }
        if (groupIntroduce.value?.useful() == false) {
            ToastUtil.showToast(app, "请输入群聊简介")
            return
        }
        NettyClient.sendMessage(
            CreateGroupRequest(
                groupName.value,
                groupAvatarUrl.value,
                "",
                groupTag.value,
                groupIntroduce.value,
                mainUser,
                users
            )
        )
    }

    fun uploads(path : String){
        viewModelScope.launch {
           val result = fileRepository.upload(path)
            if (result.isEmpty()){
                ToastUtil.showToast(app,"图片上传失败！")
            }else{
                ToastUtil.showToast(app,"图片上传成功！")
                groupAvatarUrl.postValue(result)
            }
        }
    }
}
